package zhihui.mobile.ui.shop.category;

import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.zgm.utils.ui.UiHeaderLayout;
import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.category.CategoryInfo;
import zhihui.mobile.manager.shop.CategoryManager;
import zhihui.mobile.presenter.IPresenter;
import zhihui.mobile.presenter.shop.BaseShopPresenter;
import zhihui.mobile.presenter.shop.ShopCategoryPresenter;
import zhihui.mobile.ui.BaseActivity;

/**
 * Created by 张公铭 on 2017/7/28 0028.
 */
public class ShopCategoryActivity extends BaseActivity implements IShopCategoryContract.IShopCategoryView
{
    private ShopCategoryPresenter categoryPresenter = new ShopCategoryPresenter();
    private BaseShopPresenter baseShopPresenter = new BaseShopPresenter();

    private UiHeaderLayout mHeader;
    private ListView lv_category;
    private ListView lv_content;

    @Override
    public void initView()
    {
        mHeader = (UiHeaderLayout) findViewById(R.id.header_shop_category);
        mHeader.setRootBackgroundColor("#fafafa");
        mHeader.setTitle("商品分类");
        mHeader.setTitleColor("#333333");
        mHeader.setLeftBottonIcon(R.drawable.shop_back);
        mHeader.setLeftButtonOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                goBack(ShopCategoryActivity.class);
            }
        });

        mHeader.hindRightTwoButton();
        mHeader.setRightOneBottonIcon(R.drawable.black_cart);
        mHeader.setRightOneButtonOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                categoryPresenter.goShopCart(ShopCategoryActivity.this, ShopCategoryActivity.this);
            }
        });

        lv_category = (ListView) findViewById(R.id.lv_shop_category);
        final List<CategoryInfo> categoryInfos = CategoryManager.getInstance().getmList();
        final ListAdapter adapter = new ListAdapter(this);
        adapter.setmList(categoryInfos);
        lv_category.setAdapter(adapter);
        lv_category.setSelection(0);

        lv_content = (ListView) findViewById(R.id.lv_shop_category_content);
        final CategoryAdapter categoryAdapter = new CategoryAdapter(this);
        categoryAdapter.setCategoryList(categoryInfos.get(0).getChild());
        categoryAdapter.setListener(new CategoryAdapter.ICategoryOnClickListener()
        {
            @Override
            public void goShopList(int categoryId)
            {
                baseShopPresenter.goShopCategoryActivity(ShopCategoryActivity.this, ShopCategoryActivity.this, categoryId + "");
            }
        });

        lv_content.setAdapter(categoryAdapter);
        lv_category.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                categoryAdapter.setCategoryList(categoryInfos.get(position).getChild());
                categoryAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected int getLayoutResId()
    {
        return R.layout.shop_category_activity;
    }

    @Override
    protected IPresenter[] getPresenters()
    {
        return new IPresenter[]{categoryPresenter};
    }

    @Override
    protected void onInitPresenter()
    {
        categoryPresenter.init(this, this);
    }
}
